<template>
  <div>
    <div v-if="waiting">
      请稍后
    </div>
    <div v-show="type=='1'">
      <div>
        <button @click="bind_type=1">新用户</button>
        <button @click="bind_type=2">老用户</button>
      </div>
      <div v-show="bind_type==1">
        新用户绑定
        <input type="text" v-model="email" placeholder="输入邮箱">
        <input type="password" v-model="password" placeholder="请输入密码">
        <input type="text" v-model="mail_code" placeholder="请输入邮箱验证码">
        <button @click="send_mail">获取验证码</button>
        <button @click="bind">绑定</button>
      </div>
      <div v-show="bind_type==2">
        老用户绑定
        <input type="text" v-model="email" placeholder="输入邮箱">
        <input type="password" v-model="password" placeholder="请输入密码">
        <button @click="bind">绑定</button>
      </div>
    </div>


  </div>
</template>

<script>
  import {oauth_callback, oauth_bind, send_mail} from '@/http/apis'

  export default {
    name: "Oauth_callback",
    data() {
      return {
        code: '',
        waiting: true,  // 正在请求后端,,
        type: '',  // 如果type是  1,  就展示绑定界面
        email: '',
        password: '',
        weibo_uid: '',
        mail_code: '',
        bind_type: '1', // 1. 新用户, 2. 老用户
      }
    },
    created() {
      this.code = this.$route.query.code
      this.axios.post('/oauth/weibo/', {code: this.code}).then(res => {
        debugger
        if (res.data.type == '0') {
          // 不需要绑定, 保存用户登录数据, 跳转首页
          this.waiting = false
          localStorage.setItem('username', res.data.username)
          localStorage.setItem('token', res.data.token)
          alert(res.data.msg)
          location.href = '/'
          // this.$router.push('/')

        } else {
          this.waiting = false
          // 需要绑定,  展示绑定界面, 保存 weibo_uid
          this.weibo_uid = res.data.weibo_uid
          this.type = res.data.type
        }
      })
    },
    methods: {
      bind() {
        this.axios.post('/oauth/bind/',{
          'bind_type':this.bind_type, // 绑定类型, 1,新用户, 2. 老用户
          'weibo_uid': this.weibo_uid,
          'email': this.email,
          'password': this.password,
          'mail_code': this.mail_code // 新用户传,
        }).then(res => {
          if(res.data.code=='0'){
            var data = window.atob(res.data.token.split('.')[1])
            //"{"user_id":1,"username":"admin","email":""}"
            localStorage.setItem('username', res.data.username)
            localStorage.setItem('token', res.data.token)
            localStorage.setItem('uid', data.user_id)
            this.username = res.data.username
            this.show_modal = false

            alert(res.data.msg)
            location.href = '/'
          }else{
            alert(res.data.msg)
          }

        })
      },
      send_mail() {
        this.axios.post('/user/email/',{email: this.email}).then(res => {
          if (res.data.code == 0) {
            alert(res.data.msg)
          } else {
            alert(res.data.msg)
          }
        })
      }

    }

  }
</script>

<style scoped>

</style>
